<template>
    <div class="login_containter">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 表单界面 -->
            <el-form :model="loginForm" ref="loginformref" :rules="loginrules" label-width="80px" class="login_from">
                 <el-form-item label="账号" prop="username">
                        <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
                 </el-form-item>
                 <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-s-tools
"></el-input>
                </el-form-item>
                <el-form-item class="btus">
                     <el-button type="primary" @click="login()">提交</el-button>
                     <el-button type="info" @click="restloginform()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginrules: {
        username: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    restloginform () {
    //   console.log(555)
      this.$refs.loginformref.resetFields()
    },
    login () {
      this.$refs.loginformref.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http.post('login', this.loginForm)

          if (res.meta.status !== 200) {
            // console.log('登陆失败')
            // 弹窗
            this.$message.error('登录失败')
          } else {
            this.$message.success('登陆成功')
            // console.log(res.data)
            // 将登陆信息保存到session中
            window.sessionStorage.setItem('token', res.data.token)
            // 跳转到新界面
            this.$router.push('/home')
          }
        } else {
        //   console.log(3)
        }
      })
    }
  }
}
</script>
<style  scoped>
    .login_containter{
        background-color:aqua;
        height: 100%;
    }
    .login_box{
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: #eee;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
    }
    .avatar_box>img{
        width: 100%;
        height: 100%;
         border-radius: 50%;
         background-color: #eee;
    }
    .btus{
        display: flex;
        justify-content: flex-end;
    }
    .login_from{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
</style>
